public class Exercise8 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int n = gas.length;
        int i = 0;
        int j = 0;
        while(i < n) {
            int sumGas = 0;
            int sumCost = 0;
            int tmp = 0;
            while(tmp < n) {
                j = ( i + tmp) %n;
                sumGas += gas[j];
                sumCost += cost[j];
                if(sumCost > sumGas) {
                    break;
                }
                tmp++;
            }
            if(tmp == n) {
                return i;
            }else {
                i = i + tmp + 1;
            }

        }
        return -1;
    }
}
